/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function() {


    /*$("#contactForm").bootstrapValidator({
     
     fields: {
     txtusuario: {
     validators: {
     notEmpty: {
     message: 'Ingresar Usuario'
     }
     }
     }
     
     },
     
     
     submitHandler: function(validator, form, submitButton) {
     alert("submit");
     
     }
     
     });*/



    $("#btnenviar").click(function() {
        var datosusuario = {usuario: $("#txtusuario").val(), clave: $("#txtclave").val()};
        $.ajax({
            url: "http://localhost:8080/BolsaTrabajoWS/rest/bolsatrabajo/loginempresa",
            data: JSON.stringify(datosusuario),
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {

                if (data == null) {
                    $("#error").html("Usuario y/o password erroneos");
                } else {

                    $("#hdnidempresa").val(data.idempresa);
                    $("#contactForm").attr("action", "index.jsp");
                    $("#contactForm").submit();

                }
            }

        });
    });
    
    
    $("a[rel='registrarfb']").live("click",function(){
        
       $("#contactForm").attr("action", "reg_empresa.jsp");
       $("#contactForm").submit();
    });


});

// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {

    console.log('statusChangeCallback');
    console.log(response);

    if (response.status === 'connected') {
        // Logged into your app and Facebook.

        FB.api('/me', function(response) {

            // YA SE REGISTRO ANTES ????    
            $("#txtusuario").val(response.email),
                    $("#txtclave").val("123456"),
                    datos = {
                        usuario: $("#txtusuario").val(),
                        clave: $("#txtclave").val(),
                        idpostulante: "0"
                    };
            $.ajax({
                url: "http://localhost:8080/BolsaTrabajoWS/rest/bolsatrabajo/loginempresa",
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(datos),
                success: function(data) {
                    if (data == null) {
                        cargarDatosFaceBook();

                    } else {
                        // YA SE REGISTRO 
                        $("#formlogin").attr("action", "index.jsp");
                        $("#formlogin").submit();
                    }
                }
            });
        });



    } else if (response.status === 'not_authorized') {
        // The person is logged into Facebook, but not your app.
        document.getElementById('status').innerHTML = 'Please log ' +
                'into this app.';
    } else {
        // The person is not logged into Facebook, so we're not sure if
        // they are logged into this app or not.
        document.getElementById('status').innerHTML = 'Please log ' +
                'into Facebook.';

    }
}


function checkLoginState() {
    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });
}

window.fbAsyncInit = function() {
    FB.init({
        //appId: '1441103716137580', //INGRESAR EL APP ID
        appId: '485493521583602', //INGRESAR EL APP ID
        cookie: true,
        xfbml: true,
        version: 'v2.0'
    });


    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });

};

// Load the SDK asynchronously
(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id))
        return;
    js = d.createElement(s);
    js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));


function cargarDatosFaceBook() {
 
    console.log('Cargando dats de Facebook');

    $("#cuenta").hide();
    $("#titu").text("Inicia session con faccebook");
    FB.api('/me', function(response) {
        console.log('Successful login for: ');
        console.log(JSON.stringify(response));
        console.log('Nombre    : ' + response.name);
        console.log('Email     : ' + response.email);
        console.log('Genero    : ' + response.gender);

        $("#hdnfbemresa").val(response.name);
        $("#hdnfbusuario").val(response.email);
        $("#hdnfdireccion").val(response.location.name);
        $("#hdnfbclave").val("123456");


        document.getElementById('status').innerHTML = '<a rel="registrarfb" href="#" >registrarte, ' + response.name + ' </a>';



    });



}


